﻿// ## 2023/09/04 # 123 # 贸然废立被忠臣讨伐 ##
// ## 2023/09/01 # 123 # 菜单指向修改 ##
// ## 2022/09/06 # 123 # 兼容假想玩家 ##
// ## 2022/08/21 # 123 # 增加更换汉帝机制，有铜雀玉玺折扣 ##
// ## 2020/12/12 # 江东新风 # 修复trace参数报错 ##
// ## 2020/09/21 # 江东新风 # ch::add_tp替换 ##
// ## 2020/08/08 # 氕氘氚 # 加入【国事】菜单 ##
/*
@ 제작자: HoneyBee
@ 내용: 후한황제를 폐위시키고, 세력의 군주가 황제가 됨.
//---------------------------------------------
@ 수정자: 기마책사
@ UPDATE: '18.12.3   / 변경내용: 황제폐위 후 메뉴 초기화 오류 방지 추가
*/

namespace 폐위
{

	// ================ CUSTOMIZE ================

	const int TP_COST1 = 500; 	   // 기교P 필요량
	const int ACTION_COST = 100;   // 행동력 필요량

	const int KEY = pk::hash("榷卒");

	// ===========================================

	class Main
	{

		pk::building@ building;
		pk::building@ kunshu_building;

		pk::city@ kunshu_city;
		pk::city@ emperor_city;

		pk::force@ force;

		pk::person@ kunshu;
		pk::person@ emperor;
		pk::person@ support;
		int TP_COST2 = 5000;

		Main()
		{

			pk::menu_item item;
			item.menu = state_menu::菜单_国事;
			//ch::u8debug(pk::format("menu: fei chu {}", state_menu::菜单_国事));   
			item.init = pk::building_menu_item_init_t(init);
			item.is_visible = pk::menu_item_is_visible_t(isVisible);
			item.is_enabled = pk::menu_item_is_enabled_t(isEnabled);
			item.get_text = pk::menu_item_get_text_t(getText);
			item.get_desc = pk::menu_item_get_desc_t(getDesc);
			item.handler = pk::menu_item_handler_t(handler);
			pk::add_menu_item(item);
		}


		void init(pk::building@ building)
		{

			@building = @building;
			@force = pk::get_force(building.get_force_id());
			@kunshu = pk::get_person(pk::get_kunshu_id(building));
			@kunshu_building = pk::get_building(kunshu.service);
			@kunshu_city = pk::building_to_city(kunshu_building);
			@emperor = pk::get_person(pk::get_scenario().emperor);
			if (emperor is null) return;
			else
			{
				// 황제의 SUPPORT(황후,환관)을 설정 
				if (emperor.get_id() == 武将_献帝)
					@support = pk::get_person(武将_皇后);
				else if (emperor.get_id() == 武将_少帝)
					@support = pk::get_person(武将_宦官);
				else if (emperor.get_id() == 武将_灵帝)
				{
					switch (pk::rand(2))
					{
					case 0: @support = pk::get_person(武将_张让); break;
					case 1: @support = pk::get_person(武将_蹇硕); break;
					}
				}
				@emperor_city = pk::building_to_city(pk::hex_object_to_building(pk::get_hex_object(emperor.location)));
			}
			TP_COST2 = 5000;
			pk::item@ 玉玺 = pk::get_item(宝物_玉玺);
			pk::item@ 铜雀 = pk::get_item(宝物_铜雀);
			if(玉玺.owner == kunshu.get_id())
			{
				TP_COST2 = TP_COST2 - 2500;
			}
			if(铜雀.owner == kunshu.get_id())
			{
				TP_COST2 = TP_COST2 - 1250;
			}
		}

		string getText()
		{
			return pk::encode("废黜");  //feichu
		}

		string getDesc()
		{
			if (emperor is null)
				return pk::encode("东汉灭亡了");
			else if(pk::get_scenario().event == 事件_全部发生 and (kunshu.get_id() == 武将_曹操 or kunshu.get_id() == 武将_刘备))
				return pk::encode("史实下曹刘不能贸然废立.");
			else if (!pk::is_protecting_the_emperor(force) or force.title == 爵位_皇帝)
				return pk::encode("没有拥立东汉皇帝.");
			else if (emperor_city.get_id() != kunshu.service)
				return pk::encode("君主需和东汉皇帝在同一城市.");
			else if (kunshu_city.get_id() != kunshu.service)
				return pk::encode("只能在君主所在城市实行.");
			else if (pk::is_absent(kunshu) or pk::is_unitize(kunshu) or kunshu.action_done)
				return pk::encode("君主已行动或不在城市中.");
			else if (force.tp < TP_COST1)
				return pk::encode(pk::format("技巧P不足.(技巧P {})", TP_COST1));
			else if (pk::get_district(pk::get_district_id(force, 1)).ap < ACTION_COST)
				return pk::encode(pk::format("行动力不足 (至少 {} 行动力)", ACTION_COST));
			else
				return pk::encode(pk::format("行废立之事. (技巧P{})", TP_COST1));
		}

		bool isVisible()
		{
			if (emperor is null) return false;
			else if (!pk::is_protecting_the_emperor(force) or force.title == 爵位_皇帝) return false;
			return true;
		}

		bool isEnabled()
		{
			if (emperor is null) return false;
			else if(pk::get_scenario().event == 事件_全部发生 and (kunshu.get_id() == 武将_曹操 or kunshu.get_id() == 武将_刘备)) return false;	
			else if (!pk::is_protecting_the_emperor(force) or force.title == 爵位_皇帝) return false;
			else if (emperor_city.get_id() != kunshu.service) return false;
			else if (kunshu_city.get_id() != kunshu.service) return false;
			else if (pk::is_absent(kunshu) or pk::is_unitize(kunshu) or kunshu.action_done) return false;
			else if (force.tp < TP_COST1) return false;
			else if (pk::get_district(pk::get_district_id(force, 1)).ap < ACTION_COST) return false;
			return true;
		}

		pk::random randint(pk::rand());

		bool handler()
		{

			if (pk::choose({ pk::encode("  是  "), pk::encode("  否  ") }, pk::encode("天子无能，是否行废立之事?\n"), kunshu) == 1)
			{
				return false;
			}
			string kunshu_name = pk::decode(pk::get_name(kunshu));
			bool confirm = true;
			if(pk::get_scenario().event != 事件_无 and kunshu.kanshitsu == 汉室_重视)
			{
				confirm = true;
			}
			else
			{
				confirm = pk::yes_no(pk::encode(pk::format("拥立新帝?(技巧P{})\n还是废帝自立?(技巧P{})",TP_COST1,TP_COST2)));
			}
			if (confirm == true)
			{
				pk::scene(pk::scene_t(scene_폐위_2));
				string e1= pk::decode(pk::get_name(emperor));
				emperor.sei = pk::get_person(pk::rand(670)).mei;
				emperor.birth = pk::get_year() - 7;
				emperor.death = emperor.birth + randint(50, 75);
				emperor.update();
				string e2= pk::decode(pk::get_name(emperor));
				pk::history_log(force.get_pos(), force.color, pk::encode(pk::format("\x1b[1x{}\x1b[0x废\x1b[1x{}\x1b[0x为庶人,另选宗室为帝,尊号\x1b[1x{}\x1b[0x,其权势盈天，诸侯不敢侧目.",kunshu_name,e1,e2)));
				pk::message_box(pk::encode("士兵增加了\x1b[1x10000\x1b[0x."));
				ch::add_troops(kunshu_city,10000, true);
				pk::message_box(pk::encode("气力增加了\x1b[1x50\x1b[0x."));
				pk::add_energy(kunshu_city,50, true);
				ch::add_tp(force, -TP_COST1, force.get_pos());
			}
			else//废帝
			{
				if(int(force.tp) < int(TP_COST2))
				{
					pk::message_box(pk::encode(pk::format("技巧P不足{}.",TP_COST2)));
					return false;
				}
				pk::scene(pk::scene_t(scene_폐위));
				pk::kill(emperor, kunshu, kunshu_building, null, 0);
				// 군주의 작위를 황제로 설정
				force.title = 爵位_皇帝;
				kunshu.rank=80;//兼容汉官
				kunshu.update();
				// 국호 설정
				pk::select_kokugou(force,-1);
				force.update();
				pk::create_effect(0x51, kunshu_building.pos);   //이펙트효과
				pk::message_box(pk::encode(pk::format("\x1b[1x{}\x1b[0x废汉称帝了.", kunshu_name)));
				ch::add_tp(force, -5000, force.get_pos());
				
			}
			//讨伐事件：手下重视汉室的太守会讨伐
			//获取太守列表
			pk::list<pk::person@> list_t = pk::get_person_list(force, pk::mibun_flags(身份_太守));
			list_t.sort(function(a, b)
            			{
				return a.stat[武将能力_魅力]  > b.stat[武将能力_魅力] ;// 按魅力排序
			});
			if(list_t.count > 0)
			{
				for(int i=0;i<list_t.count;i++)
				{
					if(list_t[i].kanshitsu != 汉室_重视) continue;
					pk::message_box(pk::encode(pk::format("\x1b[1x{}\x1b[0x贸然废立天子，随我起兵讨伐!",pk::decode(pk::get_name(kunshu)))),list_t[i]);
					pk::hanran(list_t[i], false);//被抽中的太守独立
					list_t[i].rank == 80;
					list_t[i].update();
					pk::force@ force_new= pk::get_force(list_t[i].get_force_id());
					五贼外交(force_new,-50,-50,-50,-50,-50);
					force_new.title = 7 + pk::rand(3);//爵位随机
					pk::history_log(force_new.get_pos(), force_new.color, pk::encode(pk::format("\x1b[1x{}\x1b[0x起兵讨伐\x1b[1x{}\x1b[0x.",pk::decode(pk::get_name(list_t[i])),pk::decode(pk::get_name(kunshu)))));
				}
			}
			//忠诚度变动
			int kunshu_0 = kunshu.get_id();
			auto city_list = pk::list_to_array(pk::get_city_list());
			for (int i = 0; i < int(city_list.length); i++)
			{

				pk::city@ city_ = city_list[i];

				pk::building@ building_ = pk::city_to_building(city_);

				pk::person@ taishu = pk::get_person(pk::get_taishu_id(building_));

				if (!building_.is_player())
					continue;

				auto mibun_list = pk::list_to_array(pk::get_person_list(building_, pk::mibun_flags(身份_都督, 身份_太守, 身份_一般)));

				if (pk::is_alive(taishu))
				{

					if (0 < mibun_list.length)
					{

						for (int j = 0; j < int(mibun_list.length); j++)
						{
							pk::person@ person_0 = mibun_list[j];

							if (pk::is_gikyoudai(person_0, kunshu_0) or pk::is_fuufu(person_0, kunshu_0) or pk::is_like(person_0, kunshu_0))
								continue;

							// 新 황제를 혐오하는 무장의 충성도 변화
							if (person_0.disliked[0] == kunshu.get_id() or person_0.disliked[1] == kunshu.get_id()
								or person_0.disliked[2] == kunshu.get_id() or person_0.disliked[3] == kunshu.get_id()
								or person_0.disliked[4] == kunshu.get_id())
							{
								pk::add_loyalty(person_0, -50);
							}

							// 한실_중시 충성도 변화
							if (person_0.kanshitsu == 한실_중시)
							{
								pk::add_loyalty(person_0, -15);
							}

							// 한실_보통 충성도 변화
							else if (person_0.kanshitsu == 한실_보통)
							{
								pk::add_loyalty(person_0, 0);
							}

							// 한실_무시 충성도 변화
							else if (person_0.kanshitsu == 한실_무시)
							{
								pk::add_loyalty(person_0, 15);
							}

						} // for

					} // if 

				} // if 

			} // for

			// 행동력 감소
			auto district = pk::get_district(pk::get_district_id(force, 1));
			pk::add_ap(district, -ACTION_COST);

			kunshu.action_done = true;

			return true;

		}


		void scene_폐위()
		{
			int pause = int(pk::option["MessagePause"]);
			pk::option["MessagePause"] = 100;
			pk::message_box(pk::encode("我得借此机会废黜皇帝, 夺取江山."), kunshu);
			pk::cutin(18);  // 컷인: 걸음걸이
			pk::move_screen(emperor_city.get_pos(), 2000);
			pk::event::diplomacy(emperor, support, kunshu, pk::event::diplomacy_t(scene_폐위_1));
			pk::move_screen(kunshu_city.get_pos(), 2000);

			pk::message_box(pk::encode("从今天起朕就是这个国家的皇帝!"), kunshu);
			pk::cutin(21);  // 컷인: 병사환호

			pk::option["MessagePause"] = 100;

		}

		void scene_폐위_1()
		{
			string kunshu_name = pk::decode(pk::get_name(kunshu));
			pk::message_box(pk::encode(pk::format("\x1b[1x{}\x1b[0x, 今天来有何贵干?", kunshu_name)), emperor);
			pk::message_box(pk::encode("你已经不配做这个皇帝了!"), kunshu);
			pk::message_box(pk::encode("以后我来治理这个国家!"), kunshu);
			pk::message_box(pk::encode("原来你也是个逆贼..."), emperor);
			pk::message_box(pk::encode(pk::format("陛下, 如果你忤逆了\x1b[1x{}\x1b[0x, 将会酿成大祸.", kunshu_name)), support);
			pk::message_box(pk::encode("............"), emperor);
		}
		void scene_폐위_2()
		{
			int pause = int(pk::option["MessagePause"]);
			pk::option["MessagePause"] = 100;
			pk::cutin(18);  // 컷인: 걸음걸이
			pk::move_screen(emperor_city.get_pos(), 2000);
			pk::event::diplomacy(emperor, support, kunshu, pk::event::diplomacy_t(scene_폐위_3));
			pk::move_screen(kunshu_city.get_pos(), 2000);
			pk::cutin(21);  // 컷인: 병사환호
			pk::option["MessagePause"] = 100;
		}
		void scene_폐위_3()
		{
			string kunshu_name = pk::decode(pk::get_name(kunshu));
			pk::message_box(pk::encode(pk::format("\x1b[1x{}\x1b[0x, 今天来有何贵干?", kunshu_name)), emperor);
			pk::message_box(pk::encode("臣奉先帝遗诏，废尔为庶人!"), kunshu);
			pk::message_box(pk::encode("朕何罪之有..."), emperor);
			pk::message_box(pk::encode(pk::format("陛下, 如果你忤逆了\x1b[1x{}\x1b[0x, 将会酿成大祸.", kunshu_name)), support);
			pk::message_box(pk::encode("............"), emperor);
		}
		void 五贼外交(pk::force@ force_t,int 羌,int 越,int 乌,int 蛮,int 贼)
		{
			pk::add_relations(force_t.get_id(),force_t.get_id(),100);
			pk::add_relations(force_t.get_id(),pk::get_person(武将_羌头目).get_force_id(),羌);
			pk::add_relations(force_t.get_id(),pk::get_person(武将_山越头目).get_force_id(),越);
			pk::add_relations(force_t.get_id(),pk::get_person(武将_乌丸头目).get_force_id(),乌);
			pk::add_relations(force_t.get_id(),pk::get_person(武将_南蛮头目).get_force_id(),蛮);
			pk::add_relations(force_t.get_id(),pk::get_person(武将_贼将).get_force_id(),贼);
		}
	}
	Main main;
}